/**
 * @file XListViewHeader.java
 * @create Apr 18, 2012 5:22:27 PM
 * @author Maxwin
 * @description XListView's header
 */
package com.xindaoapp.happypet.view.waterfall;


import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xindaoapp.happypet.R;

public class XListViewHeader extends LinearLayout {
    private LinearLayout mContainer;
    private ImageView mArrowImageView;
    // private ProgressBar mProgressBar;
    private TextView mHintTextView;
    private int mState = STATE_NORMAL;

    private AnimationDrawable mRotateUpAnim;
    private Animation mRotateDownAnim;

    private final int ROTATE_ANIM_DURATION = 180;

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_REFRESHING = 2;
    private TextView mHeaderTimeView;

    public XListViewHeader(Context context) {
        super(context);
        initView(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public XListViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        // 初始情况，设置下拉刷新view高度为0
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0);
        mContainer =
                (LinearLayout) LayoutInflater.from(context)
                        .inflate(R.layout.xlistview_header, null);
        // R.layout.xlistview_header, null);
        addView(mContainer, lp);
        setGravity(Gravity.BOTTOM);

        mArrowImageView = (ImageView) findViewById(R.id.xlistview_header_arrow);
        mHintTextView = (TextView) findViewById(R.id.xlistview_header_hint_textview);
        // mProgressBar = (ProgressBar) findViewById(R.id.xlistview_header_progressbar);
        // mHeaderTimeView = (TextView) findViewById(R.id.pull_to_refresh_header_time);

        /** 添加下拉刷新动画 */
        mArrowImageView.setBackgroundResource(R.anim.pulltoreference_animation);

        mRotateUpAnim = (AnimationDrawable) mArrowImageView.getBackground();
        mRotateUpAnim.setOneShot(false);


        // mRotateUpAnim =
        // new RotateAnimation(0.0f, -180.0f, Animation.RELATIVE_TO_SELF, 0.5f,
        // Animation.RELATIVE_TO_SELF, 0.5f);
        // mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        // mRotateUpAnim.setFillAfter(true);
        // mRotateDownAnim =
        // new RotateAnimation(-180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
        // Animation.RELATIVE_TO_SELF, 0.5f);
        // mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        // mRotateDownAnim.setFillAfter(true);
    }

    // public void setLastUpdatedLabel(CharSequence label) {
    // // 如果最后更新的时间的文本是空的话，隐藏前面的标题
    // mHeaderTimeView.setVisibility(TextUtils.isEmpty(label) ? View.INVISIBLE : View.VISIBLE);
    // mHeaderTimeView.setText("最后更新：\t" + label);
    // }

    public void setState(int state) {
        if (state == mState)
            return;

        // if (state == STATE_REFRESHING) { // 显示进度
        // if (!mRotateUpAnim.isRunning()) {
        // mRotateUpAnim.start();
        // }
        // // mArrowImageView.clearAnimation();
        // // mArrowImageView.setVisibility(View.INVISIBLE);
        // // mProgressBar.setVisibility(View.VISIBLE);
        // } else { // 显示箭头图片
        // if (mRotateUpAnim.isRunning()) {
        // mRotateUpAnim.stop();
        // }
        // // mArrowImageView.setVisibility(View.VISIBLE);
        // // mProgressBar.setVisibility(View.INVISIBLE);
        // }

        switch (state) {
            case STATE_NORMAL:
                if (mRotateUpAnim.isRunning()) {
                    mRotateUpAnim.stop();
                }
                // if (mState == STATE_READY) {
                // mArrowImageView.startAnimation(mRotateDownAnim);
                // }
                // if (mState == STATE_REFRESHING) {
                // mArrowImageView.clearAnimation();
                // }
                mHintTextView.setText(R.string.xlistview_header_hint_normal);
                break;
            case STATE_READY:
                if (mState != STATE_READY) {
                    // mArrowImageView.clearAnimation();
                    // mArrowImageView.startAnimation(mRotateUpAnim);
                    mHintTextView.setText(R.string.xlistview_header_hint_ready);
                    // SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm");
                    // setLastUpdatedLabel(sdf.format(new Date()));
                }
                break;
            case STATE_REFRESHING:
                mHintTextView.setText(R.string.xlistview_header_hint_loading);
                break;
            default:
        }

        mState = state;
    }

    public void setVisiableHeight(int height) {
        if (height < 0)
            height = 0;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer.getLayoutParams();
        lp.height = height;
        mContainer.setLayoutParams(lp);
        // mContainer.setPadding(0, height, 0, 0);
    }

    public int getVisiableHeight() {
        return mContainer.getHeight();
    }

}
